﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace AsteroidWar
{
    class AsteroidManager
    {
        private int screenWidth = 800;
        private int screenHeight = 600;
        //private int screenPadding = 10;

        private int asteroidFrames;
        public List<Sprite> Asteroids = new List<Sprite>();
        private Texture2D texture;
        private Random rand = new Random();
        private Rectangle initialFrame;
        private int minSpeed = 60;
        private int maxSpeed = 120;

        public void AddAsteroid()
        {
            Sprite newAsteroid = new Sprite(new Vector2(-100, -100), texture, initialFrame, Vector2.Zero);

            for (int x = 1; x < asteroidFrames; x++)
            {
                newAsteroid.AddFrame(new Rectangle(initialFrame.X + (initialFrame.Width * x), initialFrame.Y, initialFrame.Width, initialFrame.Height));
            }

            for (int x = 0; x < asteroidFrames; x++)
            {
                newAsteroid.AddFrame(new Rectangle(initialFrame.X + (initialFrame.Width * x), initialFrame.Y + initialFrame.Height, initialFrame.Width, initialFrame.Height));
            }

            //Random rotation
            newAsteroid.Rotation = (float)MathHelper.ToRadians(rand.Next(0, 360));
            newAsteroid.CollisionRadius = 20;
            Asteroids.Add(newAsteroid);
        }
        public void Clear()
        {
            Asteroids.Clear();
        }

        public AsteroidManager(
            int screenWidth,
            int screenHeight,
            int asteroidCount,
            Texture2D texture,
            Rectangle initialFrame,
            int asteroidFrames)
        {
            this.screenWidth = screenWidth;
            this.screenHeight = screenHeight;
            this.texture = texture;
            this.initialFrame = initialFrame;
            this.asteroidFrames = asteroidFrames;

            for (int i = 0; i < asteroidCount; i++)
            {
                AddAsteroid();
            }
        }

        public Vector2 randomLocation()
        {
            Vector2 location = Vector2.Zero;
            bool locationOK;
            int tryCount=0;
            do
            {
                locationOK = true;
                int position = rand.Next(0, 3);
                switch (position)
                {
                    case 1: location.X = -initialFrame.Width;
                        location.Y = rand.Next(0, screenHeight);
                        break;
                    case 2: location.X = rand.Next(0, screenWidth);
                        location.Y = -initialFrame.Height;
                        break;
                    case 3: location.X = screenWidth;
                        location.Y = rand.Next(0, screenHeight);
                        break;
                }

                foreach (Sprite asteroid in Asteroids)
                {
                    if (asteroid.IsBoxColliding(new Rectangle((int)location.X, (int)location.Y, initialFrame.Width, initialFrame.Height)))
                    {
                        locationOK = false;
                        break;
                    }
                }

                tryCount++;

                if (tryCount > 3 && locationOK == false)
                {
                    location = new Vector2(-500, -500);
                    locationOK = true;
                }
            } while (locationOK == false);

            return location;
        }

        public Vector2 randomVelocity()
        {
            Vector2 velocity = new Vector2(rand.Next(-50, 50), rand.Next(-50, 50));
            velocity.Normalize();
            velocity *= (float)rand.Next(minSpeed, maxSpeed);
            return velocity;
        }

        public bool isOnScreen(Sprite asteroid)
        {
            if (asteroid.Destination.Intersects(
                new Rectangle(0, 0, screenWidth, screenHeight))
                )
                return true;
            else
                return false;
        }

        public void Update(GameTime gametime)
        {
            foreach (Sprite asteroid in Asteroids)
            {
                asteroid.Update(gametime);
                if (!isOnScreen(asteroid))
                {
                    asteroid.Location = randomLocation();
                    asteroid.Velocity = randomVelocity();
                }
            }

            for (int x = 0; x < Asteroids.Count(); x++)
                for (int y = x + 1; y < Asteroids.Count(); y++)
                {
                    if(Asteroids[x].IsCircleColliding(Asteroids[y].Center,Asteroids[y].CollisionRadius))
                    {
                        BounceAsteroids(Asteroids[x], Asteroids[y]);
                    }
                }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (Sprite asteroid in Asteroids)
            {
                asteroid.Draw(spriteBatch, 0.7f);
            }
        }

        private void BounceAsteroids(Sprite asteroid1, Sprite asteroid2)
        {
            Vector2 normal1 = asteroid2.Center - asteroid1.Center;
            normal1.Normalize();
            Vector2 normal2 = -normal1;

            asteroid1.Velocity = Vector2.Reflect(asteroid1.Velocity, normal1);
            asteroid2.Velocity = Vector2.Reflect(asteroid2.Velocity, normal2);
        }

        public void reset()
        {
            foreach (Sprite asteroid in Asteroids)
            {
                asteroid.Location = new Vector2(-500, -500);
            }
        }

        
    }
}
